package com.itech.ups.app.borrowerCredit.action;

/**
 * Desc:
 * @author: lisha 2018/6/8 10:41
 */
public class TestVolatile {
    public volatile int inc = 0;

    /**
     * 去掉synchorized volatile 将只保证线程可见性 不保证原子性
     */
    public synchronized void increase() {
        inc++;
    }

    public static void main(String[] args) {
        final TestVolatile test = new TestVolatile();
        for (int i = 0; i < 10; i++) {
            new Thread() {
                @Override
                public void run() {
                    for (int j = 0; j < 1000; j++) {
                            test.increase();
                    }
                }

                ;
            }.start();
        }

        //保证前面的线程都执行完
        while (Thread.activeCount() > 1) {
            Thread.yield();
        }
        System.out.println(test.inc);
    }
}